import java.util.Random;

public class 合并两个有序数组_快速排序 {
    public int[] sortArray(int[] nums) {
        quickSort(nums,0,nums.length - 1);
        return nums;
    }

    private void quickSort(int[] nums, int left, int right) {
        if (left >= right) {
            return;
        }
        int l = left - 1;
        int key = nums[new Random().nextInt(right - left + 1) + left];
        int r = right + 1;
        // [0,l]  [l + 1,r - 1]  [r,right]
        // [<key]     [key]        [>key]
        int i = left;
        while (i < r) {
            if (nums[i] < key) {
                swap(nums,++l,i++);
            } else if (nums[i] > key) {
                swap(nums,--r,i);
            } else {
                i++;
            }
        }
        quickSort(nums,0,l);
        quickSort(nums,r,right);
    }


    public void swap(int[] nums,int a,int b){
        int tmp = nums[a];
        nums[a] = nums[b];
        nums[b] = tmp;
    }
}
